/*
 * Author Anju
 */
package com.sdi.columbus;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import android.util.Log;

@SuppressWarnings("serial") //with this annotation we are going to hide compiler warning
public class GpsData implements Serializable {
	public GpsData(){
		
		vehicleId=0;
		
		address = "";
		customername = "";
		username = "";
		password = "";
		userType="";
		subuser="";
		companyname="";
		email="";
		status="";
		accountID="";
		userID="";
		description="";
		
		date = new Date();
	}

			
	


	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAccountID() {
		return accountID;
	}
	
	
	public void setAccountID(String accountID) {
		this.accountID = accountID;
	}
	public String getAddress() {
	    return this.address;
	}
	public String getCompanyName() {
	    return this.companyname;
	}
	public String getCustomerName() {
	    return this.customername; 
	}
	public String getEmailid() {
	    return this.email; 
	}
	public String getPhoneno() {
	    return phone;
	}
	public String[] getVehicleRegNo() {
	    return VehicleRegNo; 
	}
	public String[] getVehicleModel() {
	    return VehicleNames; 
	}
	public String[] getVehicleYOM() {
	    return VehicleYOMs; 
	}
	
	public String[] getVehicleMake() {
	    return VehicleMake; 
	}
	
	public void setUsername(String Username) {
	    this.username = Username;
	}
	public void setPassword(String Password) {
	    this.password = Password;
	}
	
	
	public int getCustomerId() {
	    return customerId;
	}
	public String getUserType() {
	    return this.userType;
	}
	public int getSize() {
	    return size;
	}	
	public int getErrorcode() {
	    return code;
	}
	public String getStatus() {
	    return this.status;
	}
	
	public String getErrorMsg() {
	    return this.loginResult;
	}
	public static String[] getVehicleIds() {
		return VehicleIds;
	}

	public static void setVehicleIds(String[] vehicleIds) {
		VehicleIds = vehicleIds;
	}

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}



	private int code;
	private String address;
	private String customername;
	private String companyname;
	private String email;
	private String status;
	private static String phone;
	private String subuser;
	
	public String username;
	public String password;
	public String accountID;
//	public String custid;
	public String userID;
	public String description;
	private Date date;
	private String loginStatus;
	private static String loginResult;
	private String UserInfo;
	private String vehicledata;
	private String vehicleinfo;
	private static int customerId;
	private static int vehicleId;
	private static String userType;
	private static int size;
	
	private static String[] VehicleNames;
	private static String[] VehicleRegNo;
	private static String[] VehicleYOMs;
//	private static int[] VehicleIds;
	private static String[] VehicleIds;
	private static String[] VehicleMake;
	

	
	public Boolean Login() {
        try {   
        	
        	GpsFetchData gpsfetch = new GpsFetchData();
        	String loginMessage = gpsfetch.Login(accountID, username, password);
        	
        	
        	JSONObject json = new JSONObject(loginMessage);
			loginStatus = json.getString("LoginResult");
			
			JSONObject json1 = new JSONObject(loginStatus);
			loginResult=json1.getString("StatusMessage");
			code=Integer.parseInt(json1.getString("ErrorCode"));
			UserInfo=json1.getString("UserInfo");
			
			JSONObject json2 = new JSONObject(UserInfo);
			
			System.out.println("Hai gpsdata says login is"+loginResult);
			    if(loginResult.equalsIgnoreCase("Success"))
			      {
			    	
			    	 
//			    	 address=json2.getString("Address");
//			    	 System.out.println("address"+address);
//			    	 
//			    	 companyname=json2.getString("Company");
//			    	 System.out.println("company"+companyname);
//			    	 
//			    	 custid=json2.getString("ClientID");
//			    	 System.out.println("customrid"+custid);
//			    	 
//			    	 customerId=Integer.parseInt(custid);
//			    	 System.out.println("customrid in integer"+customerId);
			    	 
			    	 
			    	 accountID=json2.getString("AccountID");
			    	 System.out.println("customername"+accountID);
			    	 
			    	 userID=json2.getString("UserID");
			    	 System.out.println("userID"+userID);
			    	 
			    	 description=json2.getString("Description");
			    	 System.out.println("description"+description);
			    	 
			    	 customername=json2.getString("Name");
			    	 System.out.println("customername"+customername);
			    	 
			    	 email=json2.getString("EmailID");
			    	 System.out.println("emailid"+email);
			    	 
//			    	 status=json2.getString("IsUserEnabled");
			    	 
			    	 phone=json2.getString("PhoneNumber");
			    	 System.out.println("phoneno"+phone);
			    	 
//			    	 subuser=json2.getString("IsSubUsersEnabled");
			         userType="MainUser";
			    	 getVehicleDetails();
			    	 return true;
			      }
			    else
			      {
				     return false;
			      }
            }
          catch (Exception e) 
            {
        	  System.out.println(e);
                return false;
//        	  return true;
            }
			
	}
	public void getVehicleDetails() throws JSONException, IOException
	{
            String vehicleresult="";
    	
    	// Add your data
    	
    	JSONObject user =  new JSONObject();
//    	user.put("clientID", custid);
    	user.put("accountID", accountID);
    	user.put("userID", userID);
    	
    	
    	// Create a new HttpClient and Post Header
    	
    	HttpClient client = new DefaultHttpClient();
    	URI website;
		try {
			website = new URI("http://deepam.cloudapp.net/ColumbusRESTService/ColumbusREST.svc/json/GetVehicles");
			HttpPost request = new HttpPost();
	    	request.setEntity(new StringEntity(user.toString()));
	    	request.addHeader("content-type", "application/json");      
	    	request.setURI(website);
	    	
	    	// Execute HTTP Post Request
	    	HttpResponse response = client.execute(request);
	    	HttpEntity entity = response.getEntity();
			vehicleresult = EntityUtils.toString(entity);
			System.out.println("vehicle details from gpsdata"+vehicleresult);
			
			JSONObject jsonv = new JSONObject(vehicleresult);
			vehicledata=jsonv.getString("GetVehiclesResult");
			JSONObject jsonveh = new JSONObject(vehicledata);
			vehicleinfo=jsonveh.getString("Vehicles");
			System.out.println("testing"+vehicleinfo);
			JSONArray jsonvehicle = new JSONArray(vehicleinfo);
		    size=jsonvehicle.length();
		    
		    VehicleNames=new String[size];
		    VehicleRegNo=new String[size];
//			VehicleYOMs=new String[size];
			VehicleMake=new String[size];
//			VehicleIds=new int[size] ;
			VehicleIds=new String[size];
		    
		    
		    
			System.out.println("you"+size);
			for (int i = 0; i < jsonvehicle.length(); i++)
			{
				 
	             JSONObject menuObject = jsonvehicle.getJSONObject(i);
	             
//	             VehicleYOMs[i]= menuObject.getString("YearOfManufacture");
//                 System.out.println("Vehicle Manf :"+VehicleYOMs[i]);
                 
	             VehicleNames[i]= menuObject.getString("Model");
	             System.out.println("Vehicle Names :"+VehicleNames[i]);
	             
//	             VehicleIds[i]= Integer.parseInt(menuObject.getString("VehicleID"));
	             VehicleIds[i]=menuObject.getString("VehicleID");
	             System.out.println("Vehicle Id :"+VehicleIds[i]);
	             
	             VehicleRegNo[i] = menuObject.getString("RegNumber");
	             System.out.println("Vehicle Reg No :"+VehicleRegNo[i]);
	             
	             VehicleMake[i] = menuObject.getString("Make");
	             System.out.println("Vehicle Make :"+VehicleMake[i]);
	        }
			
			
			
			
		}
		catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
 }

